//
//  ChatBubbleTableViewCell.m
//  supplyerIOS
//
//  Created by 穆相臣 on 2018/8/30.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "ChatBubbleTableViewCell.h"

@interface ChatBubbleTableViewCell ()
{
    UIImageView * avatarImageView;
    UILabel * contentTextLabel;
    UIView * textBack;
    UIImageView * contentImage;
    UIButton * audioButton;
    UILabel * audioLabel;
    
    NSString * audioUrl;
    NSString * imageUrl;
}
@end

@implementation ChatBubbleTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        avatarImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
        avatarImageView.layer.cornerRadius = 20;
        avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
        avatarImageView.clipsToBounds = YES;
        [self addSubview:avatarImageView];
        
        textBack = [[UIView alloc]initWithFrame:CGRectMake(60, 10, ScreenWidth - 90, 40)];
        textBack.backgroundColor = themeColor;
        textBack.layer.cornerRadius = 5;
        [self addSubview:textBack];
        
        contentTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, ScreenWidth - 100, 30)];
        contentTextLabel.numberOfLines = 0;
        contentTextLabel.font = [UIFont systemFontOfSize:15];
        contentTextLabel.textColor = [UIColor whiteColor];
        [textBack addSubview:contentTextLabel];
        
        contentImage = [[UIImageView alloc]initWithFrame:CGRectMake(60, 10, 200, 200)];
        [self addSubview:contentImage];
        
        audioButton = [[UIButton alloc]initWithFrame:CGRectMake(60, 10, 100, 40)];
        audioButton.backgroundColor = themeColor;
        audioButton.layer.cornerRadius = 5;
        [audioButton addTarget:self action:@selector(playAudio) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:audioButton];
        
        audioLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
        audioLabel.font = [UIFont systemFontOfSize:15];
        audioLabel.textColor = [UIColor whiteColor];
        [audioButton addSubview:audioLabel];
    }
    return self;
}

+(CGFloat)heightWithSize:(CGSize)size
{
    return 230/size.width * size.height + 10;
}

+(CGFloat)height
{
    return 50;
}

+(CGFloat)heightWithContentText:(NSString *)text
{
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - 100, 30)];
    label.font = [UIFont systemFontOfSize:15];
    label.numberOfLines = 0;
    label.text = text;
    [label sizeToFit];
    if (label.frame.size.height < 40)
    {
        return 50;
    }
    else
    {
        return label.frame.size.height + 20;
    }
}

-(void)loadData:(NSString *)text
{
    contentImage.hidden = YES;
    audioButton.hidden = YES;
    textBack.hidden = NO;
    
    contentTextLabel.frame = CGRectMake(5, 5, ScreenWidth - 100, 30);
    contentTextLabel.text = text;
    [contentTextLabel sizeToFit];
    if (contentTextLabel.frame.size.height < 30)
    {
        contentTextLabel.frame = CGRectMake(5, 5, contentTextLabel.frame.size.width, 30);
        textBack.frame = CGRectMake(60, 10, contentTextLabel.frame.size.width + 10, 40);
    }
    else
    {
        contentTextLabel.frame = CGRectMake(5, 5, contentTextLabel.frame.size.width, contentTextLabel.frame.size.height);
        textBack.frame = CGRectMake(60, 10, contentTextLabel.frame.size.width + 10, contentTextLabel.frame.size.height + 10);
    }
}

-(void)loadImage:(NSString *)image size:(CGSize)size
{
    textBack.hidden = YES;
    audioButton.hidden = YES;
    contentImage.hidden = NO;
    
    contentImage.frame = CGRectMake(60, 10, 230, 230/size.width * size.height);
//    [contentImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",imagePrefix,image]]];
}

-(void)loadAudio:(NSString *)audio lenght:(NSString *)lenght
{
    textBack.hidden = YES;
    contentImage.hidden = YES;
    audioButton.hidden = NO;
    
    audioUrl = audio;
    
    NSInteger time = lenght.integerValue;
    audioLabel.text = [NSString stringWithFormat:@"%02d:%02d",(int)time/60,(int)time%60];
}

-(void)loadUserData:(NSString *)avatar left:(BOOL)left
{


    avatar = left ? avatar : [NSString stringWithFormat:@"https://pic.xrsquare.com/%@",avatar];
    
    [avatarImageView sd_setImageWithURL:[NSURL URLWithString:avatar]];
    
    if (left)
    {
        textBack.frame = CGRectMake(60, 10, textBack.frame.size.width, textBack.frame.size.height);
        contentImage.frame = CGRectMake(60, 10, contentImage.frame.size.width, contentImage.frame.size.height);
        audioButton.frame = CGRectMake(60, 10, 100, 40);
        avatarImageView.frame = CGRectMake(10, 10, 40, 40);
    }
    else
    {
        textBack.frame = CGRectMake(ScreenWidth - 60 - textBack.frame.size.width, 10, textBack.frame.size.width, textBack.frame.size.height);
        contentImage.frame = CGRectMake(ScreenWidth - 60 - contentImage.frame.size.width, 10, contentImage.frame.size.width, contentImage.frame.size.height);
        audioButton.frame = CGRectMake(ScreenWidth - 60 - 100, 10, 100, 40);
        avatarImageView.frame = CGRectMake(ScreenWidth - 10 - 40, 10, 40, 40);
    }
}

-(void)playAudio
{
    [self.delegate playAudioWithFile:audioUrl];
}




- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
